<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        .container {
            width: 90%;
            max-width: 400px;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            text-align: center;
        }
        .input-field {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }
        .register-button {
            width: 100%;
            padding: 15px;
            margin: 10px 0;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
        .register-button:hover {
            background-color: #218838;
        }
        #registerMessage {
            margin-top: 10px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>用户注册</h1>
        <form id="registerForm">
            <input type="text" id="username" class="input-field" placeholder="用户名" required>
            <input type="password" id="password" class="input-field" placeholder="密码" required>
            <input type="password" id="confirmPassword" class="input-field" placeholder="确认密码" required>
            <input type="text" id="school" class="input-field" placeholder="学校" required>
            <input type="text" id="name" class="input-field" placeholder="姓名" required>
            <input type="text" id="class" class="input-field" placeholder="班级" required>
            <button type="submit" class="register-button">注册</button>
        </form>
        <p id="registerMessage"></p>
    </div>

    <script>
        const registerForm = document.getElementById('registerForm');
        const registerMessage = document.getElementById('registerMessage');

        registerForm.addEventListener('submit', async (e) => {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const school = document.getElementById('school').value;
            const name = document.getElementById('name').value;
            const className = document.getElementById('class').value;

            if (password !== confirmPassword) {
                registerMessage.textContent = '密码和确认密码不一致！';
                registerMessage.style.color = 'red';
                return;
            }

            try {
                const response = await fetch('http://localhost:3000/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password, school, name, class: className }),
                });

                if (response.ok) {
                    registerMessage.textContent = '注册成功，3秒后跳转到登录页面...';
                    registerMessage.style.color = 'green';
                    setTimeout(() => {
                        window.location.href = '登录.html';
                    }, 3000);
                } else {
                    const errorText = await response.text();
                    registerMessage.textContent = errorText;
                    registerMessage.style.color = 'red';
                }
            } catch (error) {
                console.error('注册请求失败:', error);
                registerMessage.textContent = '服务器错误，请稍后重试';
                registerMessage.style.color = 'red';
            }
        });
    </script>
</body>
</html>